// pages/updateinfo/updatainfo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filepath:'',
    inputValue1:0,
    banner:[],
  },

  chooseimage: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        var timestamp = new Date().getTime()
        wx.cloud.uploadFile({
          cloudPath: 'image/' + timestamp + '.jpg',
          filePath: tempFilePaths[0], // 文件路径
          success: res => {
            // get resource ID
            console.log(res.fileID)
            that.setData({
              filepath: res.fileID
            })
            wx.showToast({
              title: '上传成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: err => {
            // handle error
          }
        })
      }
    })
  },


  bindsubmitForm: function (e) {
    console.log(e)
    let that = this
    var file = e.detail.value
    var id = e.detail.target.id
    if (id == 1) {
      wx.cloud.callFunction({
        name: 'addrole',
        data: {
          index: file.index,
          image: that.data.filepath,
          name: file.name,
          rolename: file.rolename,
          sex: file.sex,
          money: file.money,
          str: file.str,
          con: file.con,
          siz: file.siz,
          dex: file.dex,
          app: file.app,
          int: file.int,
          pow: file.pow,
          edu: file.edu,
          summary: file.summary,
          HP:file.HP,
          ch:file.ch,
          zt:file.zt,
          ability:file.ability

        },
        success:res=>{
          console.log(res)
          wx.showToast({
            title: '添加成功',
          })
        },
        fail:err=>{
          console.log(err)
        }
      })

    } else if (id == 2) {
      wx.cloud.callFunction({
        name: 'update',
        data: {
          index: file.index,
          image: that.data.filepath,
          name: file.name,
          rolename: file.rolename,
          sex: file.sex,
          money: file.money,
          str: file.str,
          con: file.con,
          siz: file.siz,
          dex: file.dex,
          app: file.app,
          int: file.int,
          pow: file.pow,
          edu: file.edu,
          summary: file.summary,
          HP:file.HP,
          ch:file.ch,
          zt:file.zt,
          ability:file.ability

        },
        success:res=>{
          console.log(res)
          wx.showToast({
            title: '修改成功',
          })
        },
        fail:err=>{
          console.log(err)
        }
      })
    } else if (id == 3) {
      wx.cloud.callFunction({
        name: 'updatepic',
        data: {
          index: file.index,
          image: that.data.filepath,
          name: file.name,
          rolename: file.rolename,
          sex: file.sex,
          money: file.money,
          str: file.str,
          con: file.con,
          siz: file.siz,
          dex: file.dex,
          app: file.app,
          int: file.int,
          pow: file.pow,
          edu: file.edu,
          summary: file.summary,
          HP:file.HP,
          ch:file.ch,
          zt:file.zt,
          ability:file.ability

        },
        success:res=>{
          console.log(res)
          wx.showToast({
            title: '更改成功',
          })
        },
        fail:err=>{
          console.log(err)
        }
      })

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */

  bindinput1:function(e){
    this.data.inputValue1=e.detail.value
  },

  cal:function(e) {
    var that = this
    wx.cloud.callFunction({
      // 云函数名称
      name: 'cal',
      // 传给云函数的参数
      data: {
        money:Number(this.data.inputValue1)
      },
      success: function (res) {
        console.log(res.result.sum)
        that.setData({
          addres: res.result.sum
        })
      },
      fail: console.error
    })
   
  },

  onLoad: function() {
    

     
    const db = wx.cloud.database()
    const banner = db.collection('todos').where({_id:"79550af260a229471784ffc732aa894e"})


  banner.get().then(res=>{
    console.log(res)
    this.setData({banner:res.data})
  })
  .catch(err=>{console.log(err)})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  chooseimage: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        var timestamp = new Date().getTime()
        wx.cloud.uploadFile({
          cloudPath: 'image/' + timestamp + '.jpg',
          filePath: tempFilePaths[0], // 文件路径
          success: res => {
            // get resource ID
            console.log(res.fileID)
            that.setData({
              filepath: res.fileID
            })
            wx.showToast({
              title: '上传成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: err => {
            // handle error
          }
        })
      }
    })
  },

  hadindex:function(e){
    console.log(e)
    this.data.hadindex=e.detail.value
  },



  getname:function(e){
   
    console.log(e.detail.value)
    console.log(e)
    let that = this
    var file = e.detail.value

   this.setData({
     banner:[],
   })
    const db = wx.cloud.database()
    const banner = db.collection('todos').where({index:this.data.hadindex})


  banner.get().then(e=>{
    console.log(e)
    this.setData({banner:e.data})
  })
  .catch(err=>{console.log(err)})
  }
})